package com.xxxx.crm.controller;

import com.xxxx.crm.base.BaseController;
import com.xxxx.crm.base.ResultInfo;
import com.xxxx.crm.query.CustomerOrderQuery;
import com.xxxx.crm.service.CustomerContactService;
import com.xxxx.crm.vo.CustomerContact;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

@RequestMapping("contact")
@Controller
public class CustomerContactController extends BaseController {
    @Resource
    private CustomerContactService customerContactService;

    /**
     * 分页多条件查询客户往来记录列表
     */
    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> queryCustomerContactByParams(CustomerOrderQuery customerContactQuery) {
        return customerContactService.queryCustomerContactByParams(customerContactQuery);
    }

    /**
     * 进入添加/修改往来记录信息页面
     */
    @RequestMapping("toAddOrUpdateCustomerContactPage")
    public String toAddOrUpdateCustomerOrderPage(Model model, Integer cId, Integer id) {
        // 将客户ID设置到请求域中，给往来记录页面获取
        model.addAttribute("cId", cId);
        // 通过往来记录ID查询记录
        CustomerContact customerContact = customerContactService.selectByPrimaryKey(id);
        // 将往来记录数据设置到请求域中
        model.addAttribute("customerContact", customerContact);
        return "customer/contactAdd_update";
    }

    /**
     * 添加往来记录
     */
    @PostMapping("add")
    @ResponseBody
    public ResultInfo addCustomerContact(CustomerContact customerContact) {
        customerContactService.addCustomerContact(customerContact);
        return success("往来记录信息添加成功！");
    }

    /**
     * 更新往来记录
     */
    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateCustomerOrder(CustomerContact customerContact) {
        customerContactService.updateCustomerContact(customerContact);
        return success("往来记录信息更新成功！");
    }


    /**
     * 删除往来记录（多）
     */
    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteCustomerContact(Integer[] ids) {
        // 调用Service层的删除方法
        customerContactService.deleteCustomerContact(ids);
        return success("往来记录数据删除成功！");
    }
}
